<!--
 * @name: 
 * @Author: 刘大可
 * @Date: 2022-12-30 16:14:53
 * @LastEditors: 刘大可
 * @LastEditTime: 2023-03-29 16:28:05
-->
<template>
    <view>
        <cu-custom bgColor="bgmain" :isBack="true">
            <view slot="content">添加优惠券</view>
        </cu-custom>

        <view v-for="(item,index) in coupon" :key="index" class="item flex" :class="{cur:item.isChange}">
            <!-- <image :src="item.business_image" mode="aspectFill"></image> -->
            
            <view class="del cuIcon-close" @click="del(item)"></view>
            <view style="width: 100%;"
            
            >
                <view class="flex align-center justify-between">
                    <view>
                        {{ item.c_name }}
                        <view class="name">{{item.business_name}}</view>
                        <view class="type">{{item.type==1?'满减券':'立减券'}}</view>
                    </view>
                    <view class="price"><text class="text-df">￥</text>{{item.price}}</view>
                </view>
                <view class="flex justify-between align-center" style="border-top: 1rpx dotted #D1D1D1;padding-top: 16rpx;">
                    <text class="text-sm">全场商品通用，不可叠加使用</text>
                    <view class="row">
                        <view class="use send"  

                        v-if="item.is_calm==1"
                            @click="go('/uni_modules/school-coupon/pages/send/send?sid=' + id + '&id=' + item.o_id)"
                        >推送</view>
                        <view class="use" style="margin-left:30rpx"
                            @click="go('/uni_modules/school-coupon/pages/editor/editor?sid=' + id + '&id=' + item.o_id)"
                        >编辑</view>

                        
                        
                    </view>

                </view>
            </view>
        </view>
        <view class="submit row" @click="go('/uni_modules/school-coupon/pages/editor/editor?sid='+id)">新增</view>
        <!-- <view class="submit row" @click="submit">保存</view> -->
    </view>
</template>
<script>
var app = getApp();
import tips from "@/middleware/tools/tips";
export default {
    data() {
        return {
            coupon:[],
            id:""
        }
    },
    onLoad(options) {
        this.id=options.id
        // this.go('/uni_modules/school-coupon/pages/send/send?sid=124&id=58')

        // this.getInfo()
        // this.getComutList(options.id)
    },
    onShow() {
        this.getComutList(this.id)

    },
    methods: {

        getComutList(id=""){
            this.request("ZhBusinesss/establishlist", {
                store_id:id
               
            }).then(res => {
                console.log(res);
                if (res.data.status ==200) {
                    this.coupon=res.data.data.data
                    
                    
                }
            })
        },
        getInfo() {
            var that = this;
            app.globalData.util.request({
                url: 'ZhBusinesss/view',
                method: 'GET',
                data: {
                    s_id: uni.getStorageSync('schoolId')
                },

                success:(res)=> {
                    console.log(res.data.data);
                    if (!res.data.data.coupon_list) {
                        res.data.data.coupon_list=""
                    }
                    let isChangeArr = res.data.data.coupon_list.split(",")
                    this.id= res.data.data.business_id,
                    this.coupon = res.data.data.coupon.map(item=>{
                        console.log(isChangeArr);
                        console.log(item.o_id);
                        
                        console.log(isChangeArr.indexOf(item.o_id+""));
                        
                        return {
                            ...item,
                            isChange: isChangeArr.indexOf(item.o_id + "") > -1?true:false
                        }
                    })
                    // this.coupon_list = 
                }
            }, true);
        },
        change(i){
            this.coupon[i].isChange = !this.coupon[i].isChange
            // item.isChange = !item.isChange
        },
        submit(){

            app.globalData.util.request({
                url: 'ZhBusinesss/updateCoupon',
                method: 'GET',
                data: {
                    s_id: uni.getStorageSync('schoolId'),
                    coupon_list: this.coupon.filter(item=>item.isChange).map(item=>item.o_id).join(","),
                    business_id: this.id,


                },
                async success(res) {
                    console.log(tips);
                    if (res.data.status == 200) {
                        
                        await tips.success(res.data.msg)
                        uni.navigateBack({ delta: 1 })
                        // this.getInfo()
                    }
                }
            }, true);
        },
        del(item){
            this.tips.choose("确认删除该优惠券吗？", '二次确认', "删除").then(async res=>{
                if (res.code==200) {
                    this.request("ZhBusinesss/establishdel", {
                        store_id: this.id,
                        o_id:item.o_id
                    }).then(res => {
                        console.log(res);
                        if (res.data.status == 200) {
                            this.tips.success("删除成功")
                            this.getComutList(this.id)
                        }else{
                            this.tips.tips(res.msg)

                        }
                    })
                
                }
            })
        }
    },
}
</script>
<style lang="scss">

.item {
    background-color: white;
    margin: 30rpx;
    padding: 25rpx 20rpx;
    border-radius: 20rpx;
    position: relative;
    .del{
        position: absolute;
        top:-10rpx;
         right:-10rpx;
        //  width: 30rpx;
        //  height: 30rpx;
        //  zoom: 1.3;
         padding: 5rpx;
         background-color:#141413;
         border-radius: 100rpx;
         color: #fff;
        //  background-color: #f00;
    }
    &.cur{
        filter: grayscale(1);
    }

    image {
        width: 120rpx;
        height: 120rpx;
        border-radius: 10rpx;
        margin-right: 20rpx;
        flex-shrink: 0;
    }

    .name {
        color: #232323;
        font-size: 30rpx;
        font-weight: bold;
    }

    .type {
        color: #FF4C4D;
        font-size: 24rpx;
        line-height: 50rpx;
    }

    .price {
        color: #FF4C4D;
        font-size: 50rpx;
        font-weight: bold;
    }

    .use {
        background: linear-gradient(90deg, #FF5951 0%, #FF816F 100%);
        border-radius: 10rpx;
        color: #FFFFFF;
        font-size: 22rpx;
        padding: 10rpx 20rpx;
    }
}
.submit{
    width: 600rpx;
    margin: 75rpx;
    height: 100rpx;
    justify-content: center;
        background: linear-gradient(90deg, #FF5951 0%, #FF816F 100%);
        color: #FFFFFF;
        font-size: 36rpx;
        border-radius: 300rpx;
}
</style>